package com.fsquare.controller.paypal;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.core.checkout.service.exception.CheckoutException;
import org.broadleafcommerce.core.payment.service.exception.PaymentException;
import org.broadleafcommerce.core.pricing.service.exception.PricingException;
import org.broadleafcommerce.vendor.paypal.web.controller.BroadleafPayPalController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class PayPalController extends BroadleafPayPalController {
	
	private static final Log LOG = LogFactory.getLog(PayPalController.class);
	
//	@Resource(name="blCompositePaymentService")
//    protected CompositePaymentService compositePaymentService;
//	
	 //This is the URL that will initiate the checkout process with PayPal.
    @RequestMapping("/paypal/checkout")
    public String paypalCheckout(HttpServletRequest request) throws PaymentException {
        return super.paypalCheckout(request);
    }


    @RequestMapping("/paypal/process")
    public String paypalProcess(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam String token, @RequestParam("PayerID") String payerID) throws CheckoutException, PricingException {
        return super.paypalProcess(request, response, model, token, payerID);
    }
    
    @RequestMapping("/paypal/cancel")
    public String paypalCancel(HttpServletRequest request, HttpServletResponse response, Model model) throws CheckoutException, PricingException {
    	return "redirect:/checkout";
    }
    
//    @RequestMapping(value="/paypal/process", method = {RequestMethod.GET})
//    public String paypalProcess(ModelMap model,
//                                @RequestParam String token,
//                                @RequestParam("PayerID") String payerID,
//                                CheckoutForm checkoutForm,
//                                HttpServletRequest request) { 
//        
//    	Order order = CartState.getCart();//retrieveCartOrder(request, model);
//        PaymentInfo payPalPaymentInfo = null;
//        Map<PaymentInfo, Referenced> payments = new HashMap<PaymentInfo, Referenced>();
//        for (PaymentInfo paymentInfo : order.getPaymentInfos()) {
//            if (paymentInfo.getType() == PaymentInfoType.PAYPAL) {
//                //There should only be one payment info of type paypal in the order
//                paymentInfo.getAdditionalFields().put(MessageConstants.PAYERID, payerID);
//                paymentInfo.getAdditionalFields().put(MessageConstants.TOKEN, token);
//                payments.put(paymentInfo, paymentInfo.createEmptyReferenced());
//                payPalPaymentInfo = paymentInfo;
//                break;
//            }
//        }
//
//        order.setStatus(OrderStatus.SUBMITTED);
//        order.setSubmitDate(Calendar.getInstance().getTime());
//
//        CheckoutResponse checkoutResponse;
//        try {
//            /*
//                Grab some details about the transaction - useful if you want to
//                retrieve address information for the user
//             */
//            //PayPalDetailsRequest detailsRequest = new PayPalDetailsRequest();
//            //detailsRequest.setToken(token);
//            //detailsRequest.setMethodType(PayPalMethodType.DETAILS);
//            //PayPalDetailsResponse detailsResponse = payPalPaymentModule.getExpressCheckoutDetails(detailsRequest);
//
//            checkoutResponse = checkoutService.performCheckout(order, payments);
//
//            PaymentResponseItem responseItem = checkoutResponse.getPaymentResponse().getResponseItems().get(payPalPaymentInfo);
//            if (responseItem.getTransactionSuccess()) {
//                //Fill out a few customer values for anonymous customers
//                Customer customer = order.getCustomer();
//                if (StringUtils.isEmpty(customer.getFirstName())) {
//                    customer.setFirstName(checkoutForm.getBillingAddress().getFirstName());
//                }
//                if (StringUtils.isEmpty(customer.getLastName())) {
//                    customer.setLastName(checkoutForm.getBillingAddress().getLastName());
//                }
//                if (StringUtils.isEmpty(customer.getEmailAddress())) {
//                    customer.setEmailAddress(order.getEmailAddress());
//                }
//                customerService.saveCustomer(customer, false);
//                
//                String receiptView = null;
//                return receiptView != null ? "redirect:" + receiptView : "redirect:/orders/viewOrderConfirmation.htm?orderNumber=" + order.getOrderNumber();
//            }
//        } catch (Exception e) {
//            LOG.error("Cannot perform checkout", e);
//        }
//
//        return null;
//        
//        
//    }
}
